package com.y.web.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.math.BigDecimal;

public class JsonScientificNotationRemover {
    public static String removeScientificNotation(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(json);

        rootNode = traverseAndConvert(rootNode);

        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
    }

    private static JsonNode traverseAndConvert(JsonNode node) {
        if (node.isValueNode()) {
            if (node.isFloatingPointNumber()) {
                // 将科学计数法转换为标准浮点数表示  
                double value = node.asDouble();
                return mapper().valueToTree(new BigDecimal(value).toPlainString());
            }
        } else if (node.isObject()) {
            ObjectNode objectNode = (ObjectNode) node;
            objectNode.fields().forEachRemaining(entry -> {
                JsonNode valueNode = entry.getValue();
                objectNode.set(entry.getKey(), traverseAndConvert(valueNode));
            });
        } else if (node.isArray()) {
            node.elements().forEachRemaining(element -> {
//                ((ArrayNode) node).set(node.indexOf(element), traverseAndConvert(element));
            });
        }
        return node;
    }

    private static ObjectMapper mapper() {
        return new ObjectMapper();
    }

    public static void main(String[] args) throws IOException {
        String jsonWithScientificNotation = "{\"value\": 1.234567890123456789e+9}";
        String jsonWithoutScientificNotation = removeScientificNotation(jsonWithScientificNotation);
        System.out.println(jsonWithoutScientificNotation);
    }
}